<?php
/*
 * @ https://EasyToYou.eu - IonCube v11 Decoder Online
 * @ PHP 7.2 & 7.3
 * @ Decoder version: 1.1.6
 * @ Release: 10/08/2022
 */

// Decoded file for php version 71.
namespace app\memberdiscount\controller;

class IndexCard extends \app\ApiRest
{
    protected $config_model;
    protected $card_model;
    protected $coupon_model;
    protected $order_model;
    public function __construct(\think\App $app)
    {
        parent::__construct($app);
        $this->config_model = new \app\memberdiscount\model\Config();
        $this->card_model = new \app\memberdiscount\model\Card();
        $this->order_model = new \app\memberdiscount\model\OrderList();
        $this->coupon_model = new \app\memberdiscount\model\Coupon();
    }
    public function cardList()
    {
        $user_model = new \app\massage\model\User();
        $arr["member_discount_time"] = $user_model->where(["id" => $this->_user["id"]])->value("member_discount_time");
        $arr["member_status"] = time() < $arr["member_discount_time"] ? 1 : 0;
        $config_model = $this->config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $arr["member_discount_time"] = date("Y.m.d", $arr["member_discount_time"]);
        $arr["text"] = $config_model["text"];
        $arr["discount"] = $config_model["discount"];
        $arr["balance"] = $config_model["balance"];
        $input = $this->_param;
        $dis = ["uniacid" => $this->_uniacid, "status" => 1];
        $arr["list"] = $this->card_model->where($dis)->order("top desc,id desc")->select()->toArray();
        if(!empty($arr["list"])) {
            foreach ($arr["list"] as &$value) {
                $value["difference_price"] = round($value["init_price"] - $value["price"], 2);
            }
        }
        if($this->getUserId()) {
            $cap_dis[] = ["user_id", "=", $this->getUserId()];
            $cap_dis[] = ["status", "in", [1, 2, 3, 4]];
            $coach_model = new \app\massage\model\Coach();
            $cap_info = $coach_model->where($cap_dis)->field("status,coach_name,work_img")->order("status")->find();
            $cap_info = !empty($cap_info) ? $cap_info->toArray() : [];
            $arr["coach_status"] = !empty($cap_info) ? $cap_info["status"] : -1;
            $arr["coach_info"] = !empty($cap_info) ? $cap_info : [];
        }
        return $this->success($arr);
    }
    public function cardDiscount()
    {
        $input = $this->_param;
        $card = $this->card_model->dataInfo(["id" => $input["card_id"]]);
        if(empty($card)) {
            $this->errorMsg("套餐已下架");
        }
        $coupon = $this->card_model->cardCoupon($card["id"]);
        if(!empty($coupon)) {
            $data["coupon_discount"] = array_sum(array_column($coupon, "discount_price"));
        } else {
            $data["coupon_discount"] = 0;
        }
        return $this->success($data);
    }
    public function payOrder()
    {
        $input = $this->_input;
        $card = $this->card_model->dataInfo(["id" => $input["card_id"], "status" => 1]);
        if(empty($card)) {
            $this->errorMsg("套餐已下架");
        }
        $coupon = $this->card_model->cardCoupon($card["id"]);
        $config = $this->config_model->dataInfo(["uniacid" => $this->_uniacid]);
        $card["balance"] = $config["balance"];
        $order_insert = ["uniacid" => $this->_uniacid, "user_id" => $this->_user["id"], "order_code" => orderCode(), "pay_price" => $card["price"], "card_id" => $card["id"], "title" => $card["title"], "balance" => $card["balance"], "day" => $card["day"], "create_time" => time(), "pay_model" => $input["pay_model"], "app_pay" => $this->is_app, "coach_id" => !empty($input["card_id"]) ? $input["coach_id"] : 0];
        \think\facade\Db::startTrans();
        $this->order_model->dataAdd($order_insert);
        $order_id = $this->order_model->getLastInsID();
        if(!empty($coupon)) {
            foreach ($coupon as $key => $value) {
                $map[$key] = ["order_id" => $order_id, "coupon_id" => $value["coupon_id"], "num" => $value["num"]];
            }
            $coupon_model = new \app\memberdiscount\model\OrderCoupon();
            $coupon_model->saveAll($map);
        }
        if(0 < $card["balance"] && !empty($order_insert["coach_id"])) {
            $user_model = new \app\massage\model\User();
            $member_discount_time = $user_model->where(["id" => $this->_user["id"]])->value("member_discount_time");
            if($member_discount_time < time()) {
                $comm_model = new \app\massage\model\Commission();
                $order = $this->order_model->dataInfo(["id" => $order_id]);
                $comm_model->memberDiscountCoachCommission($order, $input["pay_model"]);
            }
        }
        \think\facade\Db::commit();
        if($order_insert["pay_price"] <= 0) {
            $this->order_model->orderResult($order_insert["order_code"], $order_insert["order_code"]);
            return $this->success(true);
        }
        if($input["pay_model"] == 3) {
            $pay_model = new \longbingcore\wxcore\PayModel($this->payConfig());
            $jsApiParameters = $pay_model->aliPay($order_insert["order_code"], $order_insert["pay_price"], "MemberdiscountOrder", 6, ["openid" => $this->getUserInfo()["openid"], "uniacid" => $this->_uniacid, "type" => "Memberdiscount", "out_trade_no" => $order_insert["order_code"], "order_id" => (string) $order_id], $order_insert["pay_price"]);
            $arr["pay_list"] = $jsApiParameters;
            $arr["order_code"] = $order_insert["order_code"];
            $arr["order_id"] = $order_id;
        } else {
            $pay_controller = new \app\shop\controller\IndexWxPay($this->app);
            $jsApiParameters = $pay_controller->createWeixinPay($this->payConfig(), $this->getUserInfo()["openid"], $this->_uniacid, "anmo", ["type" => "Memberdiscount", "out_trade_no" => $order_insert["order_code"], "order_id" => (string) $order_id], $order_insert["pay_price"]);
            $arr["pay_list"] = $jsApiParameters;
            $arr["order_id"] = $order_id;
        }
        return $this->success($arr);
    }
    public function orderList()
    {
        $input = $this->_param;
        $dis[] = ["user_id", "=", $this->_user["id"]];
        $dis[] = ["pay_type", "=", 2];
        $data = $this->order_model->dataList($dis, 10);
        if(!empty($data["data"])) {
            foreach ($data["data"] as &$v) {
                $v["over_time"] = $v["member_take_effect_time"] + $v["day"] * 86400;
                $v["member_take_effect_time"] = date("Y.m.d H:i:s", $v["member_take_effect_time"]);
                $v["over_time"] = date("Y.m.d H:i:s", $v["over_time"]);
                $v["pay_time"] = date("Y.m.d H:i:s", $v["pay_time"]);
            }
        }
        return $this->success($data);
    }
    public function orderInfo()
    {
        $input = $this->_param;
        $data = $this->order_model->dataInfo(["id" => $input["id"]]);
        $data["over_time"] = $data["member_take_effect_time"] + $data["day"] * 86400;
        $data["member_take_effect_time"] = date("Y.m.d H:i:s", $data["member_take_effect_time"]);
        $data["over_time"] = date("Y.m.d H:i:s", $data["over_time"]);
        $data["pay_time"] = date("Y.m.d H:i:s", $data["pay_time"]);
        return $this->success($data);
    }
}

?>